<?php
namespace App\Http\Controllers\Coms;

use App\Http\Controllers\Controller;
use App\Models\MSmsCode;
use App\Models\MUser;
use App\Utils\GDUtil;

/**
 * 通用功能控制器
 */
class ComsController extends Controller
{
    /**
     * 后台通用验证码
     */
    public function adminCode()
    {
        $code = GDUtil::makeCode(5);
        ss([GDUtil::YZM_ADMIN_CODE => $code['code']]);
        imagepng($code['img']);
        imagedestroy($code['img']);
    }

    /**
     * 前台通用验证码
     */
    public function userCode()
    {
        $code = GDUtil::makeCode(5);
        ss([GDUtil::YZM_USER_CODE => $code['code']]);
        imagepng($code['img']);
        imagedestroy($code['img']);
    }

    /**
     * 发送通用手机验证码
     * @param $tel  手机号
     * @param null $flag 如果不为空则为注册验证
     * @return \Illuminate\Http\JsonResponse
     */
    public function sendCommonCode($tel, $flag = null)
    {
        if (!empty($flag)) {
            //检查手机号是否已被注册
            $user = MUser::where('mobile', $tel)->first();
            if (!empty($user)) {
                $this->data = $this->makeJsCode(1, "该手机号已被注册！");
                return response()->json($this->data);
            }
        }

        if (empty($tel)) {
            $this->data = $this->makeJsCode(1, "手机号不能为空！");
            return response()->json($this->data);
        }

        preg_match('/^1(3|4|5|7|8)\d{9}$/', $tel, $match);
        if (!$match) {
            $this->data = $this->makeJsCode(1, "请输入正确的手机号格式！");
            return response()->json($this->data);
        }

        $result = MSmsCode::send_yzm(8, $tel);
        if (is_bool($result)) {
            if ($result) {
                $this->data = $this->makeJsCode(0, "验证码发送成功，请注意查收！");
            } else {
                $this->data = $this->makeJsCode(1, "验证码发送失败，请重试！");
            }
        } else {
            $this->data = $this->makeJsCode(1, "您的操作过于频繁，请在 $result 秒后重新获取验证码！");
        }
        return response()->json($this->data);
    }

    /**
     * 发送邮箱验证码
     * @param $email    邮箱
     * @return \Illuminate\Http\JsonResponse
     */
    public function sendCommonEmailCode($email)
    {
        if (empty($email)) {
            $this->data = $this->makeJsCode(1, "邮箱不能为空！");
            return response()->json($this->data);
        }

        preg_match('/^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/', $email, $match);
        if (!$match) {
            $this->data = $this->makeJsCode(1, "请输入正确的邮箱格式！");
            return response()->json($this->data);
        }

        $result = MSmsCode::send_email_code($email);

        if (is_bool($result)) {
            if ($result) {
                $this->data = $this->makeJsCode(0, "邮件发送成功，请注意查收！");
            } else {
                $this->data = $this->makeJsCode(1, "邮件发送失败，请重试！");
            }
        } else {
            $this->data = $this->makeJsCode(1, "您的操作过于频繁，请在 $result 秒后重新获取邮件！");
        }
        return response()->json($this->data);
    }
}